//
//  LzwNewPageView.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/11.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwNewPageView.h"
#import "UIImageView+WebCache.h"
#import "LzwBaseImageView.h"
#import "LzwBaseScrollView.h"

@interface LzwNewPageView ()<UIScrollViewDelegate>

@property (nonatomic, retain) LzwBaseScrollView *myNewScrollView;
@property (nonatomic, retain) LzwBaseImageView *myNewImageView;

@property (nonatomic, assign) NSInteger flag;//记录当前页码
@property (nonatomic , retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSTimer *myTimer;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger number;


@end


@implementation LzwNewPageView

-(void)dealloc{
    [_myNewImageView release];
    [_myNewScrollView release];
    [_pageControl release];
    [_myTimer release];
    [super dealloc];
    
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(TimeAction:) userInfo:nil repeats:YES];
        
    }
    return  self;
}



-(void)setArrayNewView:(NSMutableArray<LzwNewHeadModle *> *)arrayNewView{

    if (_arrayNewView != arrayNewView) {
        [_arrayNewView release];
        _arrayNewView = [arrayNewView retain];
    }
    
    [self creatNews];
}

- (void)creatNews {
    self.myNewScrollView = [[LzwBaseScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 180 * WIDTH)];
//    self.myNewScrollView.backgroundColor = [UIColor lightGrayColor];
    
    self.myNewScrollView.contentSize = CGSizeMake(self.frame.size.width * [self.arrayNewView count], self.frame.size.height);
    
    self.myNewScrollView.contentOffset = CGPointMake(self.myNewScrollView.frame.size.width, 0);
    
    self.myNewScrollView.showsHorizontalScrollIndicator = NO;
    self.myNewScrollView.pagingEnabled = YES;
    self.myNewScrollView.delegate = self;
    
    [self addSubview:self.myNewScrollView];
    [_myNewScrollView release];
    
    for (int i = 0; i < self.arrayNewView.count; i++) {
        
        self.myNewImageView = [[LzwBaseImageView alloc] initWithFrame:CGRectMake((375 * WIDTH * i) + 375 * WIDTH, 0, 375 * WIDTH, 180 * WIDTH)];
        
        NSURL *url = [NSURL URLWithString:_arrayNewView[i].photo];
        
        [self.myNewImageView sd_setImageWithURL:url placeholderImage:nil];
        
        self.myNewImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageTapAction:)];
        [self.myNewImageView addGestureRecognizer:imageTap];
        
        
        [self.myNewScrollView addSubview:self.myNewImageView];
        [_myNewImageView release];
    }

    // 取最后一张 放最前面
    if (self.arrayNewView.count == 0) {
        return;
    }
    LzwBaseImageView *imageView1 = [[LzwBaseImageView alloc] initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 180 * WIDTH)];
    NSURL *url1 = [NSURL URLWithString:[_arrayNewView objectAtIndex:self.arrayNewView.count - 1].photo];
    [imageView1 sd_setImageWithURL:url1 placeholderImage:[UIImage imageNamed:@""]];
    [self.myNewScrollView addSubview:imageView1];
    [imageView1 release];
    // 取第一张 放最后
    LzwBaseImageView *imageView2 = [[LzwBaseImageView alloc] initWithFrame:CGRectMake(375 * (self.arrayNewView.count + 1) * WIDTH, 0, 375 * WIDTH, 180 * WIDTH)];
    NSURL *url2 = [NSURL URLWithString:[_arrayNewView objectAtIndex:0].photo];
    [imageView2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@""]];
    [self.myNewScrollView addSubview:imageView2];
    [imageView2 release];
    
    id imageFirst = [self.arrayNewView firstObject];
    id imageLast = [self.arrayNewView lastObject];
    
    [self.arrayNewView insertObject:imageLast atIndex:0];
    [self.arrayNewView addObject:imageFirst];
    
    

    
    // 核心的属性, 设置滚动范围
    self.myNewScrollView.contentSize = CGSizeMake(375 * (self.arrayNewView.count + 2) * WIDTH, 180 * WIDTH);  // 左右滚
    self.myNewScrollView.contentOffset = CGPointMake(0, 0);
    [self.myNewScrollView scrollRectToVisible:CGRectMake(375 * WIDTH, 0, 375 * WIDTH, 200 * WIDTH) animated:NO];// 默认从序号1位置放第1页 ，序号0位置位置放第4页
    NSLog(@"AA%lf", WIDTH*375);
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((375 - 260) / 2 * WIDTH, self.myNewScrollView.frame.size.height - 40 * WIDTH, 260 * WIDTH, 30 * WIDTH)];
    self.pageControl.numberOfPages = self.arrayNewView.count - 2;
    [self.pageControl addTarget:self action:@selector(pageCAction:) forControlEvents:UIControlEventValueChanged];
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self addSubview:self.pageControl];
    [_pageControl release];
    
    

    //1.记录当前页码,默认为0
    self.index = 0;
    
    
}




-(void)TimeAction:(NSTimer *)timer{
    
    //使用scrollview自带的滚动动画,实现起来效果不好,有bug.所以需要自己写动画效果
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.myNewScrollView setContentOffset:CGPointMake((self.index + 1) * self.myNewScrollView.frame.size.width, 0)];
        
        
    }];
    
    self.index++;
    
    //如果滑动到最后一页
    if (self.myNewScrollView.contentOffset.x == (self.arrayNewView.count - 1) * self.myNewScrollView.frame.size.width) {
        //快速移动到第一页
        [self.myNewScrollView setContentOffset:CGPointMake(self.myNewScrollView.frame.size.width, 0)];
        
        self.index = 1;
    }
}

//UIPageControl类型对象中调用的方法
- (void)pageCAction:(UIPageControl *)page{
    
    [self.myNewScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.myNewScrollView.frame.size.width, 0) animated:YES];
}

//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.pageControl.currentPage = (scrollView.contentOffset.x - self.myNewScrollView.frame.size.width) / scrollView.frame.size.width;
    
}

//当手指接触的时候让定时器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.myTimer invalidate];
    self.myTimer = nil;
}

//当手指离开的时候开始滑动
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(TimeAction:) userInfo:nil repeats:YES];
}

//定时器在不断循环滚动,就不会减速停止,此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (self.myNewScrollView == scrollView) {
        
        //每次减速结束都让scollView所有的scrollView子视图缩放变为1
        for (UIScrollView * scrollv in scrollView.subviews) {
            if ([scrollv isKindOfClass:[UIScrollView class]]) {
                scrollv.zoomScale = 1.0;
            }
        }
        
        if (self.myNewScrollView.contentOffset.x >= self.myNewScrollView.frame.size.width * (self.arrayNewView.count - 1)) {
            
            //            NSLog(@"最后一页");//循环滚动
            [self.myNewScrollView setContentOffset:CGPointMake(self.myNewScrollView.frame.size.width, 0) animated:NO];
        }
        
        if (self.myNewScrollView.contentOffset.x <= 0) {
            
            [self.myNewScrollView setContentOffset:CGPointMake(self.myNewScrollView.frame.size.width * (self.arrayNewView.count - 2), 0) animated:NO];
        }
        
    }
    
    //不管怎么移动,保证self.index存的是当前的页码
    self.index = self.myNewScrollView.contentOffset.x / self.myNewScrollView.frame.size.width -1;
    
}

//轻拍手势
- (void)ImageTapAction:(UITapGestureRecognizer *)tap{
    
    self.number = self.pageControl.currentPage + 1;
    
    
    [self.myNewViewDelegata pushDetailPage:self.number];
    
}




@end
